local hx__leiji = fk.CreateSkill {
  name = "hx__leiji",
  tags = { Skill.Compulsory },
}


hx__leiji:addEffect(fk.FinishJudge, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(hx__leiji.name) then return end
    local room = player.room
    if target.dead then return end
    return data.card.suit ~= Card.Heart or room:getCardOwner(data.card) == nil
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.card.suit == Card.Spade then
      player:broadcastSkillInvoke(hx__leiji.name, 3)
      room:notifySkillInvoked(player, hx__leiji.name, "offensive")
      local pattern = ".|2~9|spade"
      local judge = {
        who = target,
        reason = "lightning",
        pattern = pattern,
      }
      room:judge(judge)
      if judge.card:matchPattern(pattern) then
        room:damage {
          to = target,
          damage = 3,
          damageType = fk.ThunderDamage,
          skillName = hx__leiji.name,
        }
      end
    elseif data.card.suit == Card.Heart then
      player:broadcastSkillInvoke(hx__leiji.name, 4)
      room:notifySkillInvoked(player, hx__leiji.name, "support")
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = hx__leiji.name,
        })
    elseif data.card.suit == Card.Club then
      player:broadcastSkillInvoke(hx__leiji.name, 5)
      room:notifySkillInvoked(player, hx__leiji.name, "offensive")
      room:damage {
        to = target,
        damage = 1,
        damageType = fk.ThunderDamage,
        skillName = hx__leiji.name,
      }
    elseif data.card.suit == Card.Diamond then
      player:broadcastSkillInvoke(hx__leiji.name, 6)
      room:notifySkillInvoked(player, hx__leiji.name, "support")
      player:drawCards(1, hx__leiji.name)
    end
  end,
})

hx__leiji:addEffect(fk.Damaged, {
  anim_type = "support",
  mute = true,
  priority = 2,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(hx__leiji.name) then return end
    return data.damageType ~= fk.NormalDamage
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(hx__leiji.name, math.random(4, 5))
    player.room:doAnimate("InvokeSkill", {
      name = "hx__leiji",
      player = player.id,
      skill_type = "support",
    })
    local num = data.damage
    if room:askToSkillInvoke(player, {skill_name = hx__leiji.name, prompt = "雷祭：回复"..num.."点体力，或点取消摸"..num.."张牌",}) then
      room:recover({
        who = player,
        num = num,
        recoverBy = player,
        skillName = hx__leiji.name,
      })
    else
      player:drawCards(num, hx__leiji.name)
    end
  end,
})

hx__leiji:addEffect(fk.Damaged, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(hx__leiji.name) then return end
    return target == player
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(hx__leiji.name, math.random(1, 2))
    room:notifySkillInvoked(player, hx__leiji.name, "offensive")
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      prompt = "雷祭：请选择任意一名角色令其进行一次判定",
      targets = room:getAlivePlayers(),
      skill_name = hx__leiji.name,
      cancelable = false,
    })
    room:judge {
      who = to[1],
      reason = hx__leiji.name,
      pattern = ".",
    }
  end,
})

hx__leiji:addEffect(fk.TargetSpecifying, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(hx__leiji.name) then return end
    return data.from == player and data.card.color == Card.Black and data.card
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(hx__leiji.name, math.random(1, 2))
    room:notifySkillInvoked(player, hx__leiji.name, "offensive")
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      prompt = "雷祭：请选择任意一名角色令其进行一次判定",
      targets = room:getAlivePlayers(),
      skill_name = hx__leiji.name,
      cancelable = false,
    })
    room:judge {
      who = to[1],
      reason = hx__leiji.name,
      pattern = ".",
    }
  end,
})

hx__leiji:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(hx__leiji.name) then return end
    local ids = {}
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        if move.moveReason == fk.ReasonJudge then
          local judge_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Judge)
          if judge_event then
            local skill_event = judge_event:findParent(GameEvent.SkillEffect)
            if skill_event and skill_event.data.skill.skeleton == hx__leiji then
              for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.Processing then
                  table.insertIfNeed(ids, info.cardId)
                end
              end
            end
          end
        end
      end
    end
    ids = table.filter(ids, function (id)
      return table.contains(player.room.discard_pile, id)
    end)
    ids = player.room.logic:moveCardsHoldingAreaCheck(ids)
    if #ids > 0 then
      event:setCostData(self, {cid = ids[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local id = event:getCostData(self).cid
    player:broadcastSkillInvoke(hx__leiji.name, 6)
    room:notifySkillInvoked(player, hx__leiji.name, "drawcard")
    room:obtainCard(player, id, true, fk.ReasonJustMove, player, hx__leiji.name)
  end,
})


return hx__leiji
